package au.com.helloweb.androidgamepractice;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import au.com.helloweb.androidgamepractice.surface_view.SurfaceViewTest1;
import au.com.helloweb.androidgamepractice.view.DrawText;

public class MainActivity extends ActionBarActivity {
	private Button drawTextBtn, surfaceViewBtn, planeGameBtn, planeGameBtn2;
	public float sx, sy, sz;
	private SensorManager sm;
	private MyApp myApp = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		sm.registerListener(myGravityListener,
				sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_GAME);
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		myApp = (MyApp)this.getApplicationContext();

		drawTextBtn = (Button) findViewById(R.id.drawTextBtn);
		drawTextBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, DrawText.class);
				startActivity(intent);
			}
		});

		surfaceViewBtn = (Button) findViewById(R.id.surfaceViewBtn);
		surfaceViewBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						SurfaceViewTest1.class);
				startActivity(intent);
			}
		});

		planeGameBtn = (Button) findViewById(R.id.planeGameBtn);
		planeGameBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(
						MainActivity.this,
						au.com.helloweb.androidgamepractice.planegame.MainActivity.class);
				startActivity(intent);
			}
		});

		planeGameBtn2 = (Button) findViewById(R.id.planeGameBtn2);
		planeGameBtn2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(
						MainActivity.this,
						au.com.helloweb.androidgamepractice.planegame2.MainActivity.class);
				startActivity(intent);
			}
		});
	}

	/*
	 * SensorEventListener接口的实现，需要实现两个方法 方法1 onSensorChanged 当数据变化的时候被触发调用 方法2
	 * onAccuracyChanged 当获得数据的精度发生变化的时候被调用，比如突然无法获得数据时
	 */
	final SensorEventListener myGravityListener = new SensorEventListener() {

		// 复写onSensorChanged方法
		public void onSensorChanged(SensorEvent sensorEvent) {
			if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

				// 图解中已经解释三个值的含义
				sx = sensorEvent.values[SensorManager.DATA_X];
				sy = sensorEvent.values[SensorManager.DATA_Y];
				sz = sensorEvent.values[SensorManager.DATA_Z];
				if (sx == 0) {
					myApp.setStatus(0);
				} else if (sx > 0) {
					myApp.setStatus(1);
				} else {
					myApp.setStatus(2);
				}
			}
		}

		// 复写onAccuracyChanged方法
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

	public void onPause() {
		/*
		 * 很关键的部分：注意，说明文档中提到，即使activity不可见的时候，感应器依然会继续的工作，测试的时候可以发现，没有正常的刷新频率
		 * 也会非常高，所以一定要在onPause方法中关闭触发器，否则讲耗费用户大量电量，很不负责。
		 */
		//sm.unregisterListener(myGravityListener);
		super.onPause();
	}
}
